package com.blue.base.demo.controller;

import com.blue.base.common.bean.resp.JsonResult;
import com.blue.base.demo.redis.RedisTestService;
import com.blue.base.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liulei
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTestService redisTestService;

    /**
     * 测试redis注解分布式锁
     */
    @GetMapping("lock")
    public JsonResult<Boolean> lock(Integer index, String key) throws InterruptedException {
        log.info("开始测试redis锁方法");
        StopWatch stopwatch = new StopWatch("开始测试redis锁方法");
        stopwatch.start();
        redisTestService.annoLockMethod(index, key);
        stopwatch.stop();
        log.info("结束测试redis锁方法，耗时:{}", stopwatch.getTotalTimeSeconds());
        return JsonResult.ok(true);
    }

    /**
     * 测试redis直接加、解分布式锁
     */
    @GetMapping("lock_direct")
    public JsonResult<Boolean> lockDirect(String key) throws Exception {
        log.info("开始测试redis锁方法2");
        StopWatch stopwatch = new StopWatch("开始测试redis锁方法2");
        stopwatch.start();
        redisTestService.directLockMethod(key);
        stopwatch.stop();
        log.info("结束测试redis锁方法2，耗时:{}", stopwatch.getTotalTimeSeconds());
        return JsonResult.ok(true);
    }

    /**
     * 删除key
     */
    @GetMapping("remove")
    public JsonResult<Boolean> removeKey(String key) {
        Boolean del = RedisUtils.del(key);
        return JsonResult.ok(del);
    }
}
